package com.crb.smartcard.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;


/**
 * @Title: 金额转化工具类
 * @version: V1.0
 */
public class ChangeMoneyUtil {
    /**
     * 元转分，确保price保留两位有效数字
     *
     * @return
     */
    public static int changeY2F(double price) {
        DecimalFormat df = new DecimalFormat("#.00");
        price = Double.valueOf(df.format(price));
        int money = (int) (price * 100);
        return money;
    }

    /**
     * 分转元，转换为bigDecimal
     *
     * @return
     */
    public static double changeF2Y(Number price) {
        if (null == price) {
            return 0;
        }
        return BigDecimal.valueOf(price.longValue()).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP).doubleValue();
    }

    /**
     * 分转元，转换为string，保留两位小数
     *
     * @return
     */
    public static String changeF2YString(Number price) {
        if (null == price) {
            return null;
        }
        String result = BigDecimal.valueOf(price.longValue()).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP).toString();
        if (result.contains(".")) {
            String[] strings = result.split("\\.");
            if (1 == strings[1].length()) {
                result = result + "0";
            }
        } else {
            result = result + ".00";
        }
        return result;
    }

    /**
     * 千分比转百分比
     *
     * @return
     */
    public static BigDecimal mill2Percent(Number value) {
        if (null == value) {
            return null;
        }
        return BigDecimal.valueOf(value.longValue()).divide(new BigDecimal(10));
    }

    public static String mill2PercentString(Integer value) {
        if (null == value) {
            return "";
        }
        return BigDecimal.valueOf(Long.valueOf(value)).divide(new BigDecimal(10)) + "%";
    }


}